package com.unknow.first.dapp.admin.account.utils;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.unknow.first.dapp.admin.account.constants.AccountConstants.AccountStatus;
import com.unknow.first.dapp.admin.account.constants.AccountConstants.SystemAddressType;
import com.unknow.first.dapp.admin.account.constants.ISystemAddressTypeEnum;
import com.unknow.first.dapp.admin.account.mapper.TSystemAddress;
import com.unknow.first.dapp.admin.account.service.TSystemAddressService;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import lombok.extern.slf4j.Slf4j;
import org.cloud.utils.CollectionUtil;
import org.cloud.utils.SpringContextUtil;
import org.springframework.context.annotation.Lazy;

@Slf4j
public final class SystemAddressUtil {

    private TSystemAddressService systemAddressService;

    @Lazy
    private SystemAddressUtil() {
        systemAddressService = SpringContextUtil.getBean(TSystemAddressService.class);
    }

    private final static SystemAddressUtil instance = new SystemAddressUtil();

    public static SystemAddressUtil single() {
        return instance;
    }

    public List<String> getSystemAddressList(ISystemAddressTypeEnum systemAddressType) {
        LambdaQueryWrapper<TSystemAddress> query = Wrappers.<TSystemAddress>lambdaQuery().eq(TSystemAddress::getStatus, 10)
            .eq(TSystemAddress::getAddressType, systemAddressType.getValue());
        List<TSystemAddress> systemAddressList = systemAddressService.list(query);
        if (CollectionUtil.single().isEmpty(systemAddressList)) {
            return Collections.singletonList(getDefaultSystemAddress());
        }
        return systemAddressList.stream().map(TSystemAddress::getAddress).collect(Collectors.toList());
    }

    public List<String> getSystemAddressNoDefaultList(ISystemAddressTypeEnum systemAddressType) {
        LambdaQueryWrapper<TSystemAddress> query = Wrappers.<TSystemAddress>lambdaQuery().eq(TSystemAddress::getStatus, 10)
            .eq(TSystemAddress::getAddressType, systemAddressType.getValue());
        List<TSystemAddress> systemAddressList = systemAddressService.list(query);
        if (CollectionUtil.single().isEmpty(systemAddressList)) {
            return null;
        }
        return systemAddressList.stream().map(TSystemAddress::getAddress).collect(Collectors.toList());
    }

    public String getFirstSystemAddress(ISystemAddressTypeEnum systemAddressType) {
        return getSystemAddressList(systemAddressType).get(0);
    }

    public String getFirstSystemAddressNoDefault(ISystemAddressTypeEnum systemAddressType) {
        List<String> systemAddressList = getSystemAddressNoDefaultList(systemAddressType);
        if (CollectionUtil.single().isEmpty(systemAddressList)) {
            return null;
        }
        return systemAddressList.get(0);
    }

    public String getDefaultSystemAddress() {
        LambdaQueryWrapper<TSystemAddress> query = Wrappers.<TSystemAddress>lambdaQuery().eq(TSystemAddress::getStatus, 10)
            .eq(TSystemAddress::getAddressType, SystemAddressType.SYSTEM_ACCOUNT.value).last("limit 1");
        TSystemAddress defaultSystemAddress = systemAddressService.getBaseMapper().selectOne(query);
        if (CollectionUtil.single().isEmpty(defaultSystemAddress)) {
            return "system_default_address";
        }
        return defaultSystemAddress.getAddress();
    }

    /**
     * @param address
     * @param systemAddressType
     * @param excludeForbidden  是否排除禁用的系统地址
     * @return
     */
    public Boolean isSystemAddress(final String address, ISystemAddressTypeEnum systemAddressType, final boolean excludeForbidden) {
        LambdaQueryWrapper<TSystemAddress> query = Wrappers.<TSystemAddress>lambdaQuery().eq(TSystemAddress::getAddress, address);
        if (systemAddressType != null) {
            query = query.eq(TSystemAddress::getAddressType, SystemAddressType.SYSTEM_ACCOUNT.value);
        }
        if (excludeForbidden) {
            query = query.eq(TSystemAddress::getStatus, AccountStatus.NORMAL.status);
        }
        query = query.last("limit 1");
        query.select(TSystemAddress::getId);

        return systemAddressService.getBaseMapper().selectOne(query) != null;
    }

    public Boolean isSystemAddress(final String address) {
        return isSystemAddress(address, null, false);
    }

    public Boolean isSystemAddressExcludeForbidden(final String address) {
        return isSystemAddress(address, null, true);
    }

}
